package cn.my.framework.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhangl_lc on 2017/7/31.
 */
@Service("constantData")
public class ConstantData {

    @Autowired
    private Environment environment;

    public static String contextPath;

    public static String activeProfile;

    public static boolean authenticationNeeded = true;

    public static boolean authorizationNeeded = true;

    public static boolean codeCheckEnable = false;

    public static boolean securityEnable = false;

    public static boolean errorDetailShow = false;

    public static int loginLimitTimes;

    public static String uploadPath;

    public static int layuiVersion;

    public static int pageSize = 10;

    public final static Map<String,String> codeDescMap = new HashMap<String,String>(){{
        put("401","未授权");put("403","禁止访问");put("404","资源不存在");put("500","服务器错误");
        put("1000","系统内部错误");put("1001","操作错误");put("1002","非法访问");put("1003","授权过期");
    }};

    @PostConstruct
    public void init(){
        contextPath = environment.getProperty("server.context-path","/");
        activeProfile = environment.getProperty("spring.profiles.active","prod");
        authenticationNeeded = Boolean.parseBoolean(environment.getProperty("server.auth.authentication.needed","true"));
        authorizationNeeded = Boolean.parseBoolean(environment.getProperty("server.auth.authorization.needed","true"));
        codeCheckEnable = Boolean.parseBoolean(environment.getProperty("server.checkcode.enable","true"));
        securityEnable = Boolean.parseBoolean(environment.getProperty("management.security.enabled","false"));
        errorDetailShow =  Boolean.parseBoolean(environment.getProperty("server.error.detail.show","false"));
        loginLimitTimes = Integer.parseInt(environment.getProperty("server.login.limit.times","-1"));
        uploadPath = environment.getProperty("web.upload.path","F:\\tmp\\upload");
        layuiVersion = Integer.parseInt(environment.getProperty("layui.version","2"));
    }

}
